package com.qf.day11;
/**
 * 手写集合实现类
 */

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class MyCollection<E> implements Collection<E> {

    private Object[] data = new Object[10];

    private int size;
    private static final int DEFAULT_CAPACITY = 10;

    private int count = 0;


    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        return count == 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(E e) {
        if(size == data.length){
            int newSize = data.length*2;
            data = Arrays.copyOf(data,newSize);
        }
        // 如何保存传入的对象？
        // 放到数组的什么位置呢？ 声明一个实例变量，存放应该插入元素的位置
        // 同时这个变量也是集合中的元素的个数
        data[count++] = e;

        return true;
    }

    @Override
    public boolean remove(Object o) {
        for (int i = 0; i < size; i++) {
            if(data[i].equals(o)){
                for (int j = 0; j < size - 1; j++) {
                    data[j]= data[j+1];
                }
                size--;
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        for (int i = 0; i < size; i++) {
            if(data[i].equals(c)){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }
}
